﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Text;

namespace TierService
{
    /// <summary>
    /// 
    /// </summary>
    public class XmlHelper
    {
        /// <summary>
        /// 全局XmlDocument对象
        /// </summary>
        protected XmlDocument xdoc = new XmlDocument();
        /// <summary>
        /// XML根节点
        /// </summary>
        public XmlElement root;
        public XmlHelper()
        {

        }

        public void LoadXml(string xml)
        {
            xdoc.LoadXml(xml);
            root = (XmlElement)xdoc.FirstChild;
        }
                
        /// <summary>
        /// 取得结点的值
        /// </summary>
        /// <param name="name">节点名称</param>
        /// <returns>返回节点的值</returns>
        public string GetValue(string name)
        {
            XmlNode xn = FindXnByName(root.ChildNodes, name);
            if (xn == null) return null;
            return xn.InnerText;
        }
                
        /// <summary>
        /// 创建一个指定根节点的XmlDocument
        /// </summary>
        /// <param name="rootName">根节点的引用</param>
        public void CreateRoot(string rootName)
        {
            XmlElement xe = xdoc.CreateElement(rootName);
            xdoc.AppendChild(xe);
            root = xe;
        }
                
        /// <summary>
        /// 增加一个子结点
        /// </summary>
        /// <param name="name">节点名称</param>
        /// <param name="_value">节点的值</param>
        /// <returns>节点</returns>
        public XmlElement AppendChild(string name, string _value)
        {
            return AddChild((XmlElement)root, name, _value);
        }

        /// <summary>
        /// 将XML文档以字符串形式输出
        /// </summary>
        /// <returns>XML文档字符串</returns>
        public override string ToString()
        {
            return xdoc.OuterXml;
        }
                
        /// <summary>
        /// 为一个Xml元素添加子节点并设置一个值，返回添加的子节点引用
        /// </summary>
        /// <param name="xe">要添加的XmlElement对象</param>
        /// <param name="sField">子节点名</param>
        /// <param name="sValue">子节点值</param>
        /// <returns>添加的子节点引用</returns>
        public XmlElement AddChild(XmlElement xe, string sField, string sValue)
        {
            XmlElement xeTemp = xdoc.CreateElement(sField);
            xeTemp.InnerText = sValue;
            xe.AppendChild(xeTemp);
            return xeTemp;
        }
                
        /// <summary>
        /// 为一个Xml元素添加子节点但不设置值，返回添加的子节点引用
        /// </summary>
        /// <param name="xe">要添加的XmlElement对象</param>
        /// <param name="xd">XmlDocument对象</param>
        /// <param name="sField"></param>
        /// <returns>添加的子节点引用</returns>
        protected XmlElement AddChild(XmlElement xe, XmlDocument xd, string sField)
        {
            XmlElement xeTemp = xd.CreateElement(sField);
            xe.AppendChild(xeTemp);
            return xeTemp;
        }

        
        /// <summary>
        /// 为一个节点添加属性
        /// </summary>
        /// <param name="xe">XmlElement对象</param>
        /// <param name="strName">属性名称</param>
        /// <param name="strValue">属性值</param>
        public void AddAttribute(XmlElement xe, string strName, string strValue)
        {
            //判断属性是否存在
            string s = GetXaValue(xe.Attributes, strName);
            //属性已经存在
            if (s != null)
            {
                throw new System.Exception("attribute exists");
            }
            XmlAttribute xa = xdoc.CreateAttribute(strName);
            xa.Value = strValue;
            xe.Attributes.Append(xa);
        }

        /// <summary>
        /// 为一个节点添加属性，不是系统表
        /// </summary>
        /// <param name="xdoc">XmlDocument对象</param>
        /// <param name="xe">XmlElement对象</param>
        /// <param name="strName">属性名</param>
        /// <param name="strValue">属性值</param>
        protected void AddAttribute(XmlDocument xdoc, XmlElement xe, string strName, string strValue)
        {
            //判断属性是否存在
            string s = GetXaValue(xe.Attributes, strName);
            //属性已经存在
            if (s != null)
            {
                throw new Exception("Error:The attribute '" + strName + "' has been existed!");
            }
            XmlAttribute xa = xdoc.CreateAttribute(strName);
            xa.Value = strValue;
            xe.Attributes.Append(xa);
        }

        /// <summary>
        /// 通过节点名称找到指定的节点
        /// </summary>
        /// <param name="xnl">节点的引用XmlNodeList</param>
        /// <param name="strName">节点名称</param>
        /// <returns>指定的节点</returns>
        protected XmlNode FindXnByName(XmlNodeList xnl, string strName)
        {
            for (int i = 0; i < xnl.Count; i++)
            {
                if (xnl.Item(i).LocalName == strName) return xnl.Item(i);
            }
            return null;
        }

        /// <summary>
        /// 找到指定名称属性的值
        /// </summary>
        /// <param name="xac">属性的集合</param>
        /// <param name="strName">属性名称</param>
        /// <returns>相应属性的值</returns>
        protected string GetXaValue(XmlAttributeCollection xac, string strName)
        {
            for (int i = 0; i < xac.Count; i++)
            {
                if (xac.Item(i).LocalName == strName) return xac.Item(i).Value;
            }
            return null;
        }

        /// <summary>
        /// 找到指定名称属性的值
        /// </summary>
        /// <param name="xnl">结节集合</param>
        /// <param name="strName">节点名称</param>
        /// <returns>节点的值</returns>
        protected string GetXnValue(XmlNodeList xnl, string strName)
        {
            for (int i = 0; i < xnl.Count; i++)
            {
                if (xnl.Item(i).LocalName == strName) return xnl.Item(i).InnerText;
            }
            return null;
        }

        /// <summary>
        /// 设置一个节点的值
        /// </summary>
        /// <param name="xnl">节点集合</param>
        /// <param name="strName">节点名称</param>
        /// <param name="strValue">节点值</param>
        protected void SetXnValue(XmlNodeList xnl, string strName, string strValue)
        {
            for (int i = 0; i < xnl.Count; i++)
            {
                if (xnl.Item(i).LocalName == strName)
                {
                    xnl.Item(i).InnerText = strValue;
                    return;
                }
            }
            return;
        }

        /// <summary>
        /// 设置一个属性的值
        /// </summary>
        /// <param name="xac">属性的集合</param>
        /// <param name="strName">属性名称</param>
        /// <param name="strValue">属性值</param>
        protected void SetXaValue(XmlAttributeCollection xac, string strName, string strValue)
        {
            for (int i = 0; i < xac.Count; i++)
            {
                if (xac.Item(i).LocalName == strName)
                {
                    xac.Item(i).Value = strValue;
                    return;
                }
            }
            return;
        }


        /// <summary>
        /// 寻找具有指定名称和属性/值组合的节点
        /// </summary>
        /// <param name="xnl">结节集合</param>
        /// <param name="strXaName">节点名称</param>
        /// <param name="strXaValue">节点的值</param>
        /// <returns>节点的引用</returns>
        protected XmlNode FindXnByXa(XmlNodeList xnl, string strXaName, string strXaValue)
        {
            string xa;
            for (int i = 0; i < xnl.Count; i++)
            {
                xa = GetXaValue(xnl.Item(i).Attributes, strXaName);
                if (xa != null)
                {
                    if (xa == strXaValue) return xnl.Item(i);
                }
            }
            return null;
        }
    }
}